package model;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


import dao.GetCnn;
import dao.common;
import entity.Sanpham;

public class SanphamModel {
	public List<Sanpham> timsanpham(String strSearch) throws SQLException{
		List<Sanpham> listsp = new ArrayList<Sanpham>();   	   	
    	Connection cnn = GetCnn.getCnn();
		Statement stat = cnn.createStatement();
		
		ResultSet rs = stat.executeQuery(strSearch);
		while (rs.next()){
			Sanpham entity = new Sanpham();
			entity.setSoluongdamua(rs.getString("soluongdamua"));
			entity.setTensp(rs.getString("TENSP"));
			entity.setMota(rs.getString("MOTA"));
			entity.setGiaban(rs.getString("GIABAN"));
			entity.setDiscount(rs.getString("DISCOUNT"));
			entity.setUrlhinh(rs.getString("URLHINH"));
			entity.setUrlhinh2(rs.getString("URLHINH2"));
			entity.setUrlhinh3(rs.getString("URLHINH3"));
			entity.setMasp(rs.getString("MASP"));
			entity.setMadanhmuc(rs.getString("MADANHMUC"));
			entity.setThuonghieu(rs.getString("THUONGHIEU"));
			entity.setSoluong(rs.getString("SOLUONG"));
			listsp.add(entity);
		}
		return listsp;
    }
	
	public int getTotalRows(String strTong) throws SQLException {
        Connection cnn = GetCnn.getCnn();
        int count = 0;
        if (cnn != null) {
            Statement st = cnn.createStatement();
            ResultSet rs = st.executeQuery(strTong);
            while (rs.next()) {
                count++;
            }
        }
        return count;
    }
	public String get1column(String strSearch) throws SQLException{
		String kq = "";
		Connection cnn = GetCnn.getCnn();
		Statement stat = cnn.createStatement();
		ResultSet rs = stat.executeQuery(strSearch);
		while(rs.next())
			kq = rs.getString("MAKH");
		return kq;
	}
	public String getOptions(boolean isSearch, boolean isRequired, String selectedValue) {   

	    String sOptions = "";   
	    String sSel = "";   

	    if (isSearch) {   
	        sOptions += "<option value=\"\">All</option>";   
	    } else {   
	        if (!isRequired) {   
	            sOptions += "<option value=\"\"></option>";   
	        }   
	    }   
	    try {   
	    	String select = "select distinct THUONGHIEU from SANPHAM where THUONGHIEU is not null";
			Connection cnn = GetCnn.getCnn();
			Statement stat = cnn.createStatement();
			ResultSet rs = stat.executeQuery(select); 
	        while (rs.next()) {   
	            String id =common.toHTML(rs.getString(1)); 
	            if (id.compareTo(selectedValue) == 0) {   
	                sSel = "SELECTED";   
	            } else {   
	                sSel = "";   
	            }   
	            sOptions += "<option value=\"" + id + "\" " + sSel + ">" + id   
	                    + "</option>";   
	        }   
	        rs.close();   
	        stat.close();   
	    } catch (Exception e) {   
	    }   
	    return sOptions;   
	}

}
